package pcb.control.commands 
{
	import com.intel.patterns.cairngorm.CairngormEvent;
	import com.intel.utils.Debugger;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	/**
	 * ...
	 * @author Fintan Boyle
	 */
	public class Cmd_onSlideComplete extends AbstractCommand 
	{
		
		public function Cmd_onSlideComplete() 
		{
			
		}
		
		override public function execute(event:CairngormEvent):void 
		{
			
			Debugger.traceOut("Cmd_onSlideComplete :: execute()");
			
			var delay:Number = models.playerSettings.slideChangeDelay;
			
			if (delay > 0) {
				var timer = new Timer(delay, 0);
				timer.addEventListener(TimerEvent.TIMER, timer_nextPage);	
				timer.start();
			}else {
				nextPage();
			}
			
		}
		
		private function timer_nextPage(e:TimerEvent):void 
		{
			nextPage();
			e.target.stop();
			e.target.removeEventListener(TimerEvent.TIMER, timer_nextPage);
		}
		
		private function nextPage():void 
		{
			if (models.playerSettings.autoSlideTransition) {
				
				models.lessonStateModel.nextPage();
				
			}else if (models.playerSettings.playAudio && 
					  models.lessonStateModel.currentSlideIndex == models.lessonStateModel.getTotalSlides()){
				//When lesson has audio and the last slide is complete
				//the end of dialog needs to appear even when autoSlideTransition is false
				models.lessonStateModel.nextPage();
				Debugger.traceOut("the end of dialog needs to appear even when autoSlideTransition is false");
			}else {
				models.lessonStateModel.endOfSlideChk();
				models.lessonStateModel.chkLessonEnded();
			}
		}
		
	}
	
}